package main

import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"path/filepath"
	"sync"

	"github.com/student414/ddns/instance"
)

func main() {
	wg := sync.WaitGroup{}

	// Main
	filepath.Walk("./", func(path string, info fs.FileInfo, err error) error {
		extension := filepath.Ext(path)
		if extension == ".yaml" || extension == ".yml" {
			fmt.Printf("[EXEC  ]\t%s\n", path)

			// make instance
			content, err := ioutil.ReadFile(path)
			if nil != err {
				panic(err)
			}

			ins := &instance.DdnsInstanceBase{}
			ins.Parse(content)

			switch ins.Type {
			case "http_get":
				ins_http_get := new(instance.TypeHttpGet)
				ins_http_get.Parse(content)

				wg.Add(1)
				go func() {
					ins_http_get.Run()
					wg.Done()
				}()
			case "http_post":
				println("Not support yet !")
			default:
				panic("Cannot get type")
			}
		}
		return nil
	})

	wg.Wait()
}
